package com.jy.wms.service;

import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsPdaShipmentMapper;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsPdaShipmentService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Yjs
 * @Date Tue Feb 06 10:50:00 GMT+08:00 2018
 */

@Service
public class WmsPdaShipmentService extends BaseService<WmsPdaShipmentMapper, WmsPdaShipment> {
    /**
     * 分页查询发运任务信息
     * @param entity 实体
     * @return Page<WmsPdaShipment>
     */
    @BaseServiceDescribe(moduleName = "pda发运管理", btnName = "分页查询pda发运任务信息")
    public Page<WmsPdaShipment> baseSelectPage(WmsPdaShipment entity) throws Exception {
        try{
            PageHelper.startPage(entity.getPage(),entity.getRows());
            return (Page<WmsPdaShipment>)dao.baseSelectPage(entity);
        }catch(Exception e){
            throw new ServiceException("查询pda发运任务信息失败", e);
        }
    }

    /**
     * 下发发运任务
     *
     * @param wmsPdaShipmentList
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "pda发运管理", btnName = "下发发运任务")
    @Transactional
    public void addPda(List<WmsPdaShipment> wmsPdaShipmentList, String hzid, String ckid) throws Exception {
        //回滚pda下发发运任务
        List<String> pdaString = new ArrayList<>();
        //回滚拣货表:发运任务下发状态
        List<String> xfztString = new ArrayList<>();
        //查询货主信息
        WmsOwner wmsOwner = dao.searchWmsOwner(hzid);
        WmsPdaMsg wmsPdaMsg = null;
        //循环发运订单信息
        for (WmsPdaShipment wmsPdaShipment : wmsPdaShipmentList) {
            //查询发运订单信息(状态(0、初始 1、部分分配2、全部分配 3、部分发运4、全部发运5、订单取消6、订单完成))
            WmsShipmentOrder wmsShipmentOrder = dao.searchOrder(wmsPdaShipment.getFyid());
            if ("4".equals(wmsShipmentOrder.getFyzt())) {
                block(pdaString, xfztString);
                throw new ServiceException("发运编号" + wmsShipmentOrder.getFybh() + ",已全部发运不可执行下发任务");
            } else {
                //拣货状态（0、初始 1、部分拣货 2、全部拣货）
                if ("0".equals(wmsShipmentOrder.getJhzt()) || "1".equals(wmsShipmentOrder.getJhzt())) {
                    block(pdaString, xfztString);
                    throw new ServiceException("请选择拣货状态为全部拣货的数据");
                } else if ("1".equals(wmsShipmentOrder.getFyzt())) {
                    block(pdaString, xfztString);
                    throw new ServiceException("请选择发运状态为全部分配的数据");
                } else if("2".equals(wmsShipmentOrder.getFyzt()) || "3".equals(wmsShipmentOrder.getFyzt())){
                    //查询拣货明细信息(发运状态 0:初始;发运任务下发状态 0：初始 )
                    List<WmsPdaShipment> wmsPdaShipmentsList = dao.searchPicking(wmsPdaShipment.getFyid());
                    //判断货主是否需要复核(出库复核 0否 1是)
                    if ("0".equals(wmsOwner.getCkfh())) {
                        //循环拣货明细信息
                        for (WmsPdaShipment wmsPdaShipment1 : wmsPdaShipmentsList) {
                            wmsPdaShipment1.setId(UUIDUtils.uuid());
                            //作业人员ID
                            wmsPdaShipment1.setZyryid(wmsPdaShipment.getZyryid());
                            //作业人员名称
                            wmsPdaShipment1.setZyrymc(wmsPdaShipment.getZyrymc());
                            //货主id
                            wmsPdaShipment1.setHzid(hzid);
                            //仓库id
                            wmsPdaShipment1.setCkid(ckid);
                            if(!"".equals(wmsPdaShipment1.getCyr()) && wmsPdaShipment1.getCyr()!=null){

                            }else{
                                wmsPdaShipment1.setCyr("0");
                            }
                            if(!"".equals(wmsPdaShipment1.getKhdz()) && wmsPdaShipment1.getKhdz()!=null){

                            }else{
                                wmsPdaShipment1.setKhdz("0");
                            }
                            wmsPdaShipment1.setCkid(ckid);
                            //添加pda发运任务
                            Integer i = dao.addPda(wmsPdaShipment1);
                            if (i > 0) {
                                pdaString.add(wmsPdaShipment1.getId());
                                //更改拣货表:发运任务下发状态 0：初始 1：已下发
                                i = dao.updateXfzt(wmsPdaShipment1.getJhid(), "1");
                                if (i > 0) {
                                    xfztString.add(wmsPdaShipment1.getJhid());
                                } else {
                                    block(pdaString, xfztString);
                                    throw new ServiceException("更改下发发运任务状态失败");
                                }
                            } else {
                                block(pdaString, xfztString);
                                throw new ServiceException("下发发运任务失败");
                            }
                        }
                    } else {
                        //循环拣货明细信息
                        for (WmsPdaShipment wmsPdaShipment1 : wmsPdaShipmentsList) {
                            //复核状态 0：未复核 1：已复核
                            if ("1".equals(wmsPdaShipment1.getFhzt())) {
                                wmsPdaShipment1.setId(UUIDUtils.uuid());
                                //作业人员ID
                                wmsPdaShipment1.setZyryid(wmsPdaShipment.getZyryid());
                                //作业人员名称
                                wmsPdaShipment1.setZyrymc(wmsPdaShipment.getZyrymc());
                                //货主id
                                wmsPdaShipment1.setHzid(hzid);
                                //仓库id
                                wmsPdaShipment1.setCkid(ckid);
                                //添加pda发运任务
                                Integer i = dao.addPda(wmsPdaShipment1);
                                if (i > 0) {
                                    pdaString.add(wmsPdaShipment1.getId());
                                    //更改拣货表:发运任务下发状态 0：初始 1：已下发
                                    i = dao.updateXfzt(wmsPdaShipment1.getJhid(), "1");
                                    if (i > 0) {
                                        xfztString.add(wmsPdaShipment1.getJhid());
                                    } else {
                                        block(pdaString, xfztString);
                                        throw new ServiceException("更改下发发运任务状态失败");
                                    }
                                } else {
                                    block(pdaString, xfztString);
                                    throw new ServiceException("下发发运任务失败");
                                }
                            } else if (!"0".equals(wmsPdaShipment1.getSszxid())) {
                                //整箱的拣货记录将不需要复核就可以发运(查询是否为整箱)
                                WmsBox wmsBox = dao.searchBox4(wmsPdaShipment1.getSszxid());
                                //整散类型 0整箱 1散箱
                                if ("0".equals(wmsBox.getZslx())) {
                                    wmsPdaShipment1.setId(UUIDUtils.uuid());
                                    //作业人员ID
                                    wmsPdaShipment1.setZyryid(wmsPdaShipment.getZyryid());
                                    //作业人员名称
                                    wmsPdaShipment1.setZyrymc(wmsPdaShipment.getZyrymc());
                                    //货主id
                                    wmsPdaShipment1.setHzid(hzid);
                                    //仓库id
                                    wmsPdaShipment1.setCkid(ckid);
                                    //添加pda发运任务
                                    Integer i = dao.addPda(wmsPdaShipment1);
                                    if (i > 0) {
                                        pdaString.add(wmsPdaShipment1.getId());
                                        //更改拣货表:发运任务下发状态 0：初始 1：已下发
                                        i = dao.updateXfzt(wmsPdaShipment1.getJhid(), "1");
                                        if (i > 0) {
                                            xfztString.add(wmsPdaShipment1.getJhid());
                                        } else {
                                            block(pdaString, xfztString);
                                            throw new ServiceException("更改下发发运任务状态失败");
                                        }
                                    } else {
                                        block(pdaString, xfztString);
                                        throw new ServiceException("下发发运任务失败");
                                    }
                                } else {
                                    throw new ServiceException(wmsShipmentOrder.getFybh() + "发运订单散箱未复核");
                                }
                            } else {
                                throw new ServiceException(wmsShipmentOrder.getFybh() + "发运订单未复核成功");
                            }
                        }
                    }
                }else{
                    throw new ServiceException("请选择发运状态为全部分配/部分发运的数据");
                }
            }
            saveMsg(wmsPdaMsg, wmsPdaShipment.getZyryid(), wmsShipmentOrder.getFybh());
        }
    }

    /**
     * PDA消息推送
     *
     * @param wmsPdaMsg
     * @param ryid
     * @param fybh
     * @throws Exception
     */
    public void saveMsg(WmsPdaMsg wmsPdaMsg, String ryid, String fybh) throws Exception {
        //pda信息推送表添加数据
        wmsPdaMsg = new WmsPdaMsg();
        //标题
        wmsPdaMsg.setBt(fybh);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = new Date();
        String bcTime = sdf.format(time);
        //消息
        wmsPdaMsg.setXx(bcTime + "生成下发发运任务");
        //状态
        wmsPdaMsg.setZt("0");
        //人员id
        wmsPdaMsg.setRyid(ryid);
        Integer i = dao.saveMsg(wmsPdaMsg);
        if (i <= 0) {
            throw new ServiceException("PDA下发发运任务消息推送失败");
        }
    }
    /**
     * 下发发运任务错误回滚
     *
     * @param pdaString
     * @param xfztString
     */
    public void block(List<String> pdaString, List<String> xfztString) {
        if (pdaString != null && pdaString.size() > 0) {
            //删除pda记录
            dao.deletePda(pdaString);
        }
        if (xfztString != null && xfztString.size() > 0) {
            //回滚拣货表下发状态记录
            for (String id : xfztString) {
                dao.updateXfzt(id, "0");
            }
        }
    }
}
